package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.UserManager;
import com.itheima.app.manager.VideoManager;
import com.itheima.domain.db.User;
import com.itheima.domain.vo.PageBeanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class VideoController {
    @Autowired //注入自己的代码管理类
    private VideoManager videoManager;
    //小视频列表
    @GetMapping("/smallVideos")
    public PageBeanVo findSmallVideos(
            @RequestParam(defaultValue = "1") Integer page ,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){

        return videoManager.findSmallVideos(page , pageSize);
    }
    //小视频上传
    @PostMapping("/smallVideos")
    public Map saveSmallVideos(
           MultipartFile videoThumbnail ,  //封面
           MultipartFile videoFile // 视频
    ) throws IOException {

        return videoManager.saveSmallVideos(videoThumbnail , videoFile);
    }



    //关注
    @PostMapping("/smallVideos/{videoUserId}/userFocus")
    public Map userFocus(
            @PathVariable Long videoUserId
    ) throws IOException {

        return videoManager.saveUserFocus(videoUserId);
    }

    //取消关注
    @PostMapping("/smallVideos/{videoUserId}/userUnFocus")
    public Map userUnFocus(
            @PathVariable Long videoUserId
    ) throws IOException {

        return videoManager.removeUserUnFocus(videoUserId);
    }

    //视频点赞
    @PostMapping("/smallVideos/{videoUserId}/like")
    public Integer likeVideo(
            @PathVariable String videoUserId
    ) throws IOException {

        return videoManager.likeVideo(videoUserId);
    }

    //视频取消点赞
    @PostMapping("/smallVideos/{videoUserId}/dislike")
    public Integer dislikeVideo(
            @PathVariable String videoUserId
    ) throws IOException {

        return videoManager.dislikeVideo(videoUserId);
    }

    //视频发表评论
    @PostMapping("/smallVideos/{videoUserId}/comments")
    public Integer saveVideoComment(
            @PathVariable String videoUserId,
            @RequestBody Map<String,String> map
    ) throws IOException {

        String videoComment = map.get("comment");

        return videoManager.saveVideoComment(videoUserId,videoComment);
    }

    //视频评论列表查询
    @GetMapping("/smallVideos/{videoUserId}/comments")
    public PageBeanVo findVideoCommentList(
            @PathVariable String videoUserId,
            @RequestParam(defaultValue = "1") Integer page ,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){

        return videoManager.findVideoCommentList(page , pageSize,videoUserId);
    }

    //视频评论点赞
    @PostMapping("/smallVideos/comments/{commentId}/like")
    public Integer videoCommentLike(@PathVariable String commentId) {
        //1.获得用户id
        Long userId = UserHolder.getId();
        //2.调用manager
        return videoManager.videoCommentLike(userId, commentId);
    }

    //视频评论点赞取消
    @PostMapping("/smallVideos/comments/{commentId}/dislike")
    public Integer videoCommentNotLike(@PathVariable String commentId) {
        //1.获得用户id
        Long userId = UserHolder.getId();
        //2.调用manager
        return videoManager.videoCommentNotLike(userId, commentId);
    }

}
